package com.slf.engine.services.impl;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.w3c.dom.ls.LSException;

import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.IZgkwService;
import com.slf.engine.utils.KeyedDigestMD5;

public class ZgkwServiceImpl implements IZgkwService {

	private static final Logger log = Logger.getLogger(ZgkwServiceImpl.class);
	
	public Order doPay(Order order)
	{
		StringBuffer url = new StringBuffer(SysContants.ZGKW_URL);
		url.append("?do=2010|");
		url.append(SysContants.ZGKW_LOGINNAME + "|");
		url.append(order.getAccount() + "|");
		url.append(order.getMoney() + "|");
		url.append(order.getOrderId() + "|");
		url.append(KeyedDigestMD5.getKeyedDigest("2010" + SysContants.ZGKW_LOGINNAME + order.getAccount() 
				+ order.getMoney() + order.getOrderId() + SysContants.ZGKW_KEY, ""));
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "充值提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", url.toString(), SysContants.DEFAULT_CHARSET, null,  null,  null,  null,  null);
			String body = new String(wrapper.getBody(), SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "充值返回：" + body);
			String s[] = body.split("\\|");
			if(s != null && s.length > 0)
			{
				if("2000".equals(s[0]) || "9007".equals(s[0]))
				{
					order.setClbz(SysContants.CZ_RUN);
				}else{
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg(s[1] + s[2]);
				}
			}else{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("充值提交，上游返回异常");
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doPay(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		}
		
		return order;
	}
	
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer(SysContants.ZGKW_URL);
		url.append("?do=3010|");
		url.append(SysContants.ZGKW_LOGINNAME + "|");
		url.append(order.getOrderId() + "|");
		url.append(KeyedDigestMD5.getKeyedDigest("3010" + SysContants.ZGKW_LOGINNAME + order.getOrderId() + SysContants.ZGKW_KEY, ""));
		HttpClientManager manager = new HttpClientManager();
		try {
			log.log(Priority.INFO, "查询提交：" + url.toString());
			HttpResponseWrapper wrapper = manager.execute("GET", url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "查询返回：" + body);
			String s[] = body.split("\\|");
			if(s != null && s.length > 0)
			{
				if("3000".equals(s[0]))
				{
					if("16".equals(s[3]))
					{
						order.setClbz(SysContants.CZ_SUCCESS);
						order.setCljg("充值成功");
					}else if("6".equals(s[3]) || "0".equals(s[3])){
						order.setClbz(SysContants.CZ_RUN);
					}else{
						order.setClbz(SysContants.CZ_FAILURE);
						order.setCljg(body);
					}
				}else{
					order.setClbz(SysContants.CZ_UNKNOW);
					order.setCljg("上游订单查询失败");
				}
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doQuery(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		}
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("13858001723");
		order.setMoney("20");
		order.setOrderId("1123123123");
		new ZgkwServiceImpl().doQuery(order);
	}
}
